﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Configuration;
using Code.Models;
using System.Security.Cryptography;
using Code.BLL;

namespace QianBuo.Lottery.Server
{
    public class AccessData
    {
        /// <summary>
        /// 检查连接是否正常
        /// </summary>
        /// <returns></returns>
        public bool CheckConnection()
        {
            return new SiteManage().CheckConnection();
        }
        /// <summary>
        /// 更新注册码
        /// </summary>
        /// <param name="regestCode">注册码</param>
        /// <param name="clearFirst">是否清除之前的注册</param>
        /// <returns></returns>
        /// <exception cref="AccessDataException"></exception>
        public int Regest_Update(string dnsName,string regestCode, bool clearFirst)
        {
            if (string.IsNullOrEmpty(dnsName) || string.IsNullOrEmpty(regestCode))
                return -1;
            var sm=new SiteManage();
            var falg = sm.Select(s => s.Any(p => p.Key == "Site_RegestCode"));
            if (falg)
            {
                var update = new NameValue<string, object>();
                if (clearFirst)
                    update.Add("Value", dnsName + "#" + regestCode);
                else
                {
                    var old = Regest_Get();
                    var reg = new System.Text.RegularExpressions.Regex("^" + dnsName + @"\(\d\)");
                    var list = old.Keys.Where(p => reg.IsMatch(p) || p == dnsName).ToList();
                    if (list != null && list.Count > 0)
                        dnsName += "(" + list.Count + ")";
                    update.Add("Value", new OpteratorValue(Opterator.Add, "&" + dnsName + "#" + regestCode));
                }
                var where = new NameValue<string, object>
                {
                    {"Key","Site_RegestCode"}
                };
                return sm.Update(update, where);
            }
            else
                return sm.Insert(new NameValue<string, object> {{"Key","Site_RegestCode"},{"Value",dnsName+"#"+regestCode},{"Remark","注册码"} });
        }

        /// <summary>
        /// 删除已注册的域名
        /// </summary>
        /// <param name="dnsName"></param>
        /// <param name="regestCode"></param>
        /// <returns></returns>
        public int Regest_Delete(string dnsName, string regestCode)
        {
            if (string.IsNullOrEmpty(dnsName))
                return -1;
            if (string.IsNullOrEmpty(regestCode))
                return -1;
            var update = new NameValue<string, object>
            {
                {"Value",new SqlExpression("replace(replace([Value],'"+dnsName+"#"+regestCode+"',''),'&&','&')")}
            };
            var where = new NameValue<string, object>
            {
                {"Key","Site_RegestCode"}
            };
            return new SiteManage().Update(update, where);
        }

        /// <summary>
        /// 获取所有注册的域名
        /// </summary>
        /// <returns></returns>
        public Dictionary<string,string> Regest_Get()
        {
            var option_Site = new SiteManage().Select(s => s.FirstOrDefault(p => p.Key == "Site_RegestCode"));
            if (option_Site == null)
                return null;
            var value = option_Site.Value;
            if (value != null)
            {
                var list = new Dictionary<string, string>();
                var values = value.Split('&');
                foreach (var item in values)
                {
                    var ss=item.Split('#');
                    if (string.IsNullOrEmpty(ss[0]))
                        continue;
                    if (list.ContainsKey(ss[0]))
                        list[ss[0]] = ss[1];
                    else
                        list.Add(ss[0], ss[1]);
                }
                return list;
            }
            return null;
        }
    }

    public class AccessDataException : Exception
    {
        public AccessDataException()
        { }
        public AccessDataException(string message)
            : base(message)
        { }
        public AccessDataException(string message,Exception innerException)
            : base(message, innerException)
        { }
        public AccessDataException(System.Runtime.Serialization.SerializationInfo info,System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
}
